$(".projectBox,.discountBoxHide").hide();
$(function () {
    getPaySeriveFreeService();
});

function payServiceSearch() {
    var reach = $("#payServiceNames").val().trim();
    var treatmentUnit = $("#treatmentUnitTwo").val();
    var reservationsId = $("#reservationsId").val();
    if (reach != "") {
        $.ajax({
            url: Feng.ctxPath + "/payService/payServiceSearch",
            type: "post",
            data: {
                reach: reach,
                treatmentUnit: treatmentUnit,
                reservationsId: reservationsId
            },
            success: function (data) {
                var patient = "";
                for (var i = 0; i < data.length; i++) {
                    // if (data[i].payServicePriceOne == data[i].payServicePriceTwo) {
                    patient += '<li style="padding: 5px 0 10px 10px;border-bottom: 1px solid #cccccc" id="' + data[i].id + '" class="' + data[i].id + '">' +
                        '<div><span>' + data[i].firstLevelProject + '</span>&nbsp;&nbsp;<span>' + data[i].payServiceName + '</span>' +
                        '<span>' + data[i].payServicePriceTwo + '</span>元<input class="department" type="hidden" value="' + data[i].department + '"></div>' +
                        '</li>'
                    //         '<span>' + data[i].payServicePriceOne + '</span>-' + data[i].payServicePriceTwo + '元' +
                }
                $("#dataUl").html(patient).show();
            },
            error: function (data) {
                alert('没有权限，请联系系统管理员!');
                parent.layer.close(parent.layer.getFrameIndex(window.name));
            }
        })
    } else {
        $("#dataUl").hide();
    }
}

$("#payServiceNames").click(function (e) {
    $.ajax({
        url: Feng.ctxPath + "/consultant/getCustomerStatus/" + $("#customerId").val() ,
        dataType: "json",
        contentType: "application/json",
        type: "POST",
        success: function (data) {
            if (data.split(",")[0] == '复诊') {
                alert("复诊不能重新开单,请去进行重咨操作！")
                return;
            }
        }
    });
    e.stopPropagation();
    payServiceSearch();
});

$(document).click(function () {
    $("#dataUl").hide();
});

$("#dataUl").on('click', 'li', function () {//点击添加开单项目
    var id = $(this).attr('class');
    var treatmentUnit = document.getElementById("treatmentUnit").value;
    var department = $(this).find(".department").val();
    if (department == treatmentUnit) {
        chooseProject(id);
    } else {
        Feng.error("所选项目为" + department + "科室项目,不在当前科室，请更改科室！");
        return false;
    }
});

$("#integral,#earnest").click(function () {
    var project = $("#projectListInfo").children().length;
    var text = "";
    if ($("#integral").is(':checked') && $("#earnest").is(':checked')) {
        $('.deleteDeal').removeAttr('onclick');
        $("#projectListInfo input").attr('readonly', true).css("border", "none");
        $("#projectListInfo .doctor").attr("disabled", "disabled").css("border", "none");
        $("#projectListInfo .reduce,#projectListInfo .add").hide();
        $(".discountBoxHide").show();
        $("#payServiceNamesd,.toothBox").css("pointer-events", "none");
        for (var i = 0; i < project; i++) {
            text += "<div class='deductionDealList'>" +
                "<div class='payServiceName' style='flex: 2'>" + $("#projectListInfo .payServiceName").eq(i).text() + "<input class='payServiceId' value='" + $("#projectListInfo .payServiceId").eq(i).val() + "' type='hidden'><input class='orderDiscount' value='" + $("#projectListInfo .discount").eq(i).val() + "' type='hidden'><input type='hidden' class='vipPayserviceId' value='" + $("#projectListInfo .vipPayserviceId").eq(i).val() + "'></div>" +
                "<div class='payService' style='flex: 0.5'>" + $("#projectListInfo .payService").eq(i).val() + "<input value='" + $("#projectListInfo .leftUp").eq(i).html() + "' class='leftUp' type='hidden' /><input value='" + $("#projectListInfo .leftDown").eq(i).html() + "' class='leftDown' type='hidden' /><input value='" + $("#projectListInfo .rightUp").eq(i).html() + "' class='rightUp' type='hidden' /><input value='" + $("#projectListInfo .rightDown").eq(i).html() + "' class='rightDown' type='hidden' /></div>" +
                "<div class='num' style='flex: 0.5'>" + $("#projectListInfo .num").eq(i).val() + "</div>" +
                "<div style='display: none;'>" +
                "<input class='discountHide' value='" + $("#projectListInfo .discountHide").eq(i).val() + "' type='hidden' />" +
                "<input class='primary' value='" + $("#projectListInfo .primary").eq(i).val() + "' type='hidden' />" +
                "</div>" +
                "<div class='amount' style='flex: 0.5'>" + $("#projectListInfo .amount").eq(i).val() + "<input class='doctor' value='" + $("#projectListInfo .doctor").eq(i).val() + "' type='hidden' /></div>" +
                "<div class='realAmount' style='flex: 0.5'>" + $("#projectListInfo .realAmount").eq(i).val() + "<input type='hidden' class=''></div>" +
                "<div><input class='depositAmount' value='0' oninput='decimalNum(this),onchangeDepositAmount(this)' type='text'><input class='depositAmount1' value='0' type='hidden'></div>" +
                "<div><select style='width: 80%' class='deductionType' name='1' onchange='applicationWay(this)'>" + checkoutMethodHtml + "</select></div>" +
                "<div><input class='deductionAmount' value='0' oninput='decimalNum(this),changeDeductionAmount(this)' type='text'><input class='deductionAmount1' value='0' type='hidden'></div>" +
                "</div>";
        }
        $("#discountBox").html(text);
        $(".projectBox,.earnest,.integral").show();
        $("#realTotalSum2").val($("#totalSum1").val());
    } else if ($("#integral").is(':checked')) {
        $('.deleteDeal').removeAttr('onclick');
        $("#projectListInfo input").attr('readonly', true).css("border", "none");
        $("#projectListInfo .doctor").attr("disabled", "disabled").css("border", "none");
        $("#projectListInfo .reduce,#projectListInfo .add").hide();
        $(".discountBoxHide").show();
        $("#payServiceNamesd,.toothBox").css("pointer-events", "none");
        for (var i = 0; i < project; i++) {
            text += "<div class='deductionDealList'>" +
                "<div class='payServiceName' style='flex: 2'>" + $("#projectListInfo .payServiceName").eq(i).text() + "<input class='payServiceId' value='" + $("#projectListInfo .payServiceId").eq(i).val() + "' type='hidden'/><input class='orderDiscount' value='" + $("#projectListInfo .discount").eq(i).val() + "' type='hidden'><input type='hidden' class='vipPayserviceId' value='" + $("#projectListInfo .vipPayserviceId").eq(i).val() + "'></div>" +
                "<div class='payService' style='flex: 0.5'>" + $("#projectListInfo .payService").eq(i).val() + "<input value='" + $("#projectListInfo .leftUp").eq(i).html() + "' class='leftUp' type='hidden' /><input value='" + $("#projectListInfo .leftDown").eq(i).html() + "' class='leftDown' type='hidden' /><input value='" + $("#projectListInfo .rightUp").eq(i).html() + "' class='rightUp' type='hidden' /><input value='" + $("#projectListInfo .rightDown").eq(i).html() + "' class='rightDown' type='hidden' /></div>" +
                "<div class='num' style='flex: 0.5'>" + $("#projectListInfo .num").eq(i).val() + "</div>" +
                "<div style='display: none;'>" +
                "<input class='discountHide' value='" + $("#projectListInfo .discountHide").eq(i).val() + "' type='hidden' />" +
                "<input class='primary' value='" + $("#projectListInfo .primary").eq(i).val() + "' type='hidden' />" +
                "</div>" +
                "<div class='amount' style='flex: 0.5'>" + $("#projectListInfo .amount").eq(i).val() + "<input class='doctor' value='" + $("#projectListInfo .doctor").eq(i).val() + "' type='hidden' /></div>" +
                "<div class='realAmount' style='flex: 0.5;color: #FF7E00'>" + $("#projectListInfo .realAmount").eq(i).val() + "</div>" +
                "<div><select style='width: 80%' class='deductionType' name='1' onchange='applicationWay(this)'>" + checkoutMethodHtml + "</select></div>" +
                "<div><input class='deductionAmount' value='0' oninput='decimalNum(this),changeDeductionAmount(this)' type='text'><input class='deductionAmount1' value='0' type='hidden'></div></div>";
        }
        $("#discountBox").html(text);
        $(".projectBox,.integral").show();
        $(".earnest").hide();
        $("#realTotalSum2").val($("#totalSum1").val());
    } else if ($("#earnest").is(':checked')) {
        $('.deleteDeal').removeAttr('onclick');
        $("#projectListInfo input").attr('readonly', true).css("border", "none");
        $("#projectListInfo .doctor").attr("disabled", "disabled").css("border", "none");
        $("#projectListInfo .reduce,#projectListInfo .add").hide();
        $(".discountBoxHide").show();
        $("#payServiceNamesd,.toothBox").css("pointer-events", "none");
        for (var i = 0; i < project; i++) {
            text += "<div class='deductionDealList'>" +
                "<div class='payServiceName' style='flex: 2'>" + $("#projectListInfo .payServiceName").eq(i).text() + "<input class='payServiceId' value='" + $("#projectListInfo .payServiceId").eq(i).val() + "' type='hidden'/><input class='orderDiscount' value='" + $("#projectListInfo .discount").eq(i).val() + "' type='hidden'/><input type='hidden' class='vipPayserviceId' value='" + $("#projectListInfo .vipPayserviceId").eq(i).val() + "'></div>" +
                "<div class='payService' style='flex: 0.5'>" + $("#projectListInfo .payService").eq(i).val() + "<input value='" + $("#projectListInfo .leftUp").eq(i).html() + "' class='leftUp' type='hidden' /><input value='" + $("#projectListInfo .leftDown").eq(i).html() + "' class='leftDown' type='hidden' /><input value='" + $("#projectListInfo .rightUp").eq(i).html() + "' class='rightUp' type='hidden' /><input value='" + $("#projectListInfo .rightDown").eq(i).html() + "' class='rightDown' type='hidden' /></div>" +
                "<div class='num' style='flex: 0.5'>" + $("#projectListInfo .num").eq(i).val() + "</div>" +
                "<div style='display: none;'>" +
                "<input class='discountHide' value='" + $("#projectListInfo .discountHide").eq(i).val() + "' type='hidden' />" +
                "<input class='primary' value='" + $("#projectListInfo .primary").eq(i).val() + "' type='hidden' />" +
                "</div>" +
                "<div class='amount' style='flex: 0.5'>" + $("#projectListInfo .amount").eq(i).val() + "</div>" +
                "<input class='doctor' value='" + $("#projectListInfo .doctor").eq(i).val() + "' type='hidden' />" +
                "<div class='realAmount' style='flex: 0.5'>" + $("#projectListInfo .realAmount").eq(i).val() + "</div>" +
                "<div><input class='depositAmount' value='0' oninput='decimalNum(this),onchangeDepositAmount(this)' type='text'><input class='depositAmount1' value='0' type='hidden' ></div></div>";
        }
        $("#discountBox").html(text);
        $(".projectBox,.earnest").show();
        $(".integral").hide();
        $("#realTotalSum2").val($("#totalSum1").val());
    } else {
        $(".deleteDeal").attr("onclick", "deleteProject(this);");
        $(".projectBox,.discountBoxHide").hide();
        $("#projectListInfo input").attr('readonly', false).css("border", "1px solid #999999");
        $("#projectListInfo .doctor").removeAttr("disabled").css("border", "1px solid #999999");
        $("#projectListInfo .reduce,#projectListInfo .add").show();
        $("#payServiceNamesd,.toothBox").css("pointer-events", "auto");
        var totalSum = 0;
        $("#projectListInfo .amount").each(function (index, obj) {
            totalSum += parseFloat($(obj).val());
        });
        $("#totalSum1").val(totalSum.toFixed(2));
        $("#realTotalSum").val(totalSum.toFixed(2));
        $("#newTotalSum").val(totalSum.toFixed(2));
        $("#newPayAmount").val(totalSum.toFixed(2));
        $("#newArrears").val(0);
        var whole = 0;
        $("#projectListInfo .primary").each(function (index, obj) {
            whole += parseFloat($(obj).val());
        });
        var wholeDiscount = parseFloat(totalSum / whole * 100).toFixed(2);
        $("#totalDiscount").val(wholeDiscount)
    }
});

$("#membershipOther").click(function () {
    if ($(".proDealInfo").length <= 0) {
        alert("未选择开单项目不能使用此功能！");
        return false;
    } else {
        if ($("#membershipOther").prop("checked") == false) {
            if (confirm("是否取消使用其他会员折扣？！")) {
                var member = window.$("#discountDiscount").val();
                window.$("#moneyBalance").html($("#moneyBalanceyw").val());
                window.$("#jfbalance").html($("#jfbalanceyw").val());
                $("#membershipOther").prop('checked', false);
                $("#membershipOther").data('waschecked', false);
                membership(member)
            }
        } else {
            $("#membershipOther").prop('checked', true);
            $("#membershipOther").data('waschecked', true);
            var index = layer.open({
                type: 2,
                title: '使用其他会员折扣',
                area: ['30%', '40%'], //宽高
                fix: true, //不固定
                maxmin: true,
                content: Feng.ctxPath + '/doctorLooked/subsequent_membershipOther'
            });
            this.layerIndex = index;
        }
    }
});

function membership(numBer) {
    var member = parseFloat(numBer);
    var dataList = $(".proDealInfo");
    for (var i = 0; i < dataList.length; i++) {
        var obj = dataList[i];
        $(obj).find(".discount").val(member * 100);
        $(obj).find(".priceDiscountRecord").val(member * 100);
        var payService = parseFloat($(obj).find(".payService").val()); //当前单价
        var num = $(obj).find(".num").val(); //数量
        $(obj).find(".discountHide").val(payService);
        var amount = parseFloat(num * payService * member).toFixed(1);
        $(obj).find(".amount").val(amount); // 折后应收
        $(obj).find(".realAmount").val(amount); // 折后应收
        $(obj).find(".primary").val(amount); // 折后价
    }
    totalAmountMoney();
}

function decimalNum(obj) {
    obj.value = obj.value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
    obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字
    obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个, 清除多余的
    obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
    if (obj.value.indexOf(".") < 0 && obj.value != "") {//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
        obj.value = parseFloat(obj.value);
    }
}

function changeDeductionAmount(obj) {//返现抵扣
    var deductionAmount = parseFloat($(obj).val());
    var jfbalance = parseFloat($("#jfbalance").html());
    var parentNode = $(obj).parent().parent();
    var deductionType = $(parentNode).find(".deductionType").find("option:selected").text();
    if (deductionType.indexOf('返现') >= 0) {
        if (jfbalance > 0) {
            var totalSum = 0;
            $("#discountBox .deductionAmount").each(function (index, obj) {
                if ($(obj).parent().parent().find('.deductionType').find("option:selected").text() == '返现抵扣') {
                    totalSum += parseFloat($(obj).val());
                }
            });
            if (isNaN(deductionAmount) != true) {
                var amount = parseFloat($(obj).parent().parent().find(".amount").html());
                if ($('#earnest').is(':checked')) {
                    var depositAmount = parseFloat($(obj).parent().parent().find(".depositAmount").val());
                    if ((deductionAmount + depositAmount) > amount) {
                        $(obj).val($(obj).siblings().val());
                    } else {
                        if ((jfbalance - totalSum) >= 0) {
                            $(obj).siblings().val(deductionAmount.toFixed(2));
                            $(obj).parent().parent().find(".realAmount").html((amount - depositAmount - deductionAmount).toFixed(2));
                        } else {
                            $(obj).val($(obj).siblings().val());
                        }
                    }
                } else {
                    if (deductionAmount > amount) {
                        $(obj).val($(obj).siblings().val());
                    } else {
                        if ((jfbalance - totalSum) >= 0) {
                            $(obj).siblings().val(deductionAmount.toFixed(2));
                            $(obj).parent().parent().find(".realAmount").html(amount - deductionAmount.toFixed(2));
                        } else {
                            $(obj).val($(obj).siblings().val());
                        }
                    }
                }
                var totalSum1 = 0;
                $("#discountBox .realAmount").each(function (index, obj) {
                    totalSum1 += parseFloat($(obj).html());
                });
                $("#realTotalSum2").val(totalSum1.toFixed(2));
                $("#newTotalSum").val(totalSum1.toFixed(2));
                $("#newPayAmount").val(totalSum1.toFixed(2));
                $("#newArrears").val((parseFloat($("#newTotalSum").val()) - parseFloat($("#newPayAmount").val())).toFixed(2));
            }
        } else {
            $(obj).val(0);
            layer.alert("没有返现可以进行抵扣！");
            return false;
        }
    } else {
        var totalSum = 0;
        $("#discountBox .deductionAmount").each(function (index, obj) {
            if ($(obj).parent().parent().find('.deductionType').find("option:selected").text() == '返现抵扣') {
                totalSum += parseFloat($(obj).val());
            }
        });
        if (isNaN(deductionAmount) != true) {
            var amount = parseFloat($(obj).parent().parent().find(".amount").html());
            if ($('#earnest').is(':checked')) {
                var depositAmount = parseFloat($(obj).parent().parent().find(".depositAmount").val());
                if ((deductionAmount + depositAmount) > amount) {
                    $(obj).val($(obj).siblings().val());
                } else {
                    if ((jfbalance - totalSum) >= 0) {
                        $(obj).siblings().val(deductionAmount.toFixed(2));
                        $(obj).parent().parent().find(".realAmount").html((amount - depositAmount - deductionAmount).toFixed(2));
                    } else {
                        $(obj).val($(obj).siblings().val());
                    }
                }
            } else {
                if (deductionAmount > amount) {
                    $(obj).val($(obj).siblings().val());
                } else {
                    if ((jfbalance - totalSum) >= 0) {
                        $(obj).siblings().val(deductionAmount.toFixed(2));
                        $(obj).parent().parent().find(".realAmount").html(amount - deductionAmount.toFixed(2));
                    } else {
                        $(obj).val($(obj).siblings().val());
                    }
                }
            }
            var totalSum = 0;
            $("#discountBox .realAmount").each(function (index, obj) {
                totalSum += parseFloat($(obj).html());
            });
            $("#realTotalSum2").val(totalSum.toFixed(2));
            $("#newTotalSum").val(totalSum.toFixed(2));
            $("#newPayAmount").val(totalSum.toFixed(2));
            $("#newArrears").val((parseFloat($("#newTotalSum").val()) - parseFloat($("#newPayAmount").val())).toFixed(2));
        }
    }
}

function onchangeDepositAmount(obj) {//储值划扣
    var depositAmount = parseFloat($(obj).val());
    var moneyBalance = parseFloat($("#moneyBalance").html());
    if (moneyBalance > 0) {
        var totalSum = 0;
        $("#discountBox .depositAmount").each(function (index, obj) {
            totalSum += parseFloat($(obj).val());
        });
        if (isNaN(depositAmount) != true) {
            var amount = parseFloat($(obj).parent().parent().find(".amount").html());
            if ($('#integral').is(':checked')) {
                var deductionAmount = parseFloat($(obj).parent().parent().find(".deductionAmount").val());
                if ((deductionAmount + depositAmount) > amount) {
                    $(obj).val($(obj).siblings().val());
                } else {
                    if ((moneyBalance - totalSum) >= 0) {
                        $(obj).siblings().val(depositAmount.toFixed(2));
                        $(obj).parent().parent().find(".realAmount").html((amount - depositAmount - deductionAmount).toFixed(2));
                    } else {
                        $(obj).val($(obj).siblings().val());
                    }
                }
            } else {
                if (depositAmount > amount) {
                    $(obj).val($(obj).siblings().val());
                } else {
                    if ((moneyBalance - totalSum) >= 0) {
                        $(obj).siblings().val(depositAmount.toFixed(2));
                        $(obj).parent().parent().find(".realAmount").html((amount - depositAmount).toFixed(2));
                    } else {
                        $(obj).val($(obj).siblings().val());
                    }
                }
            }
            var totalSum = 0;
            $("#discountBox .realAmount").each(function (index, obj) {
                totalSum += parseFloat($(obj).html());
            });
            $("#realTotalSum2").val(totalSum.toFixed(2));
            $("#newTotalSum").val(totalSum.toFixed(2));
            $("#newPayAmount").val(totalSum.toFixed(2));
            $("#newArrears").val((parseFloat($("#newTotalSum").val()) - parseFloat($("#newPayAmount").val())).toFixed(2));
        }
    } else {
        $(obj).val(0);
        layer.alert("没有储值可以划扣");
        return false;
    }
}

function oninputPayAmount() {//改缴费
    var newTotalSum = parseFloat($("#newTotalSum").val());
    var payAmount = parseFloat($("#newPayAmount").val());
    var arrears = (newTotalSum - payAmount).toFixed(2);
    if (isNaN(payAmount) != true) {
        $("#newArrears").val(arrears);
    } else {
        $("#newPayAmount").val(newTotalSum.toFixed(2));
        $("#newArrears").val(0);
    }
    if (payAmount > newTotalSum) {
        $("#newArrears").val(0);
        $("#newPayAmount").val(newTotalSum.toFixed(2));
        layer.alert("缴费金额不应该超过应收金额！");
        return false;
    }
}

function applicationWay(obj) {
    if ($(obj).find("option:selected").text() == "使用返现") {
        $(obj).parent().parent().find(".deductionAmount").attr("readonly", false).val(0);
        var amount = parseFloat($(obj).parent().parent().find(".amount").html());
        if ($('#earnest').is(':checked')) {
            var depositAmount = parseFloat($(obj).parent().parent().find(".depositAmount").val());
            $(obj).parent().parent().find(".realAmount").html((amount - depositAmount).toFixed(2));
        } else {
            $(obj).parent().parent().find(".realAmount").html(amount.toFixed(2));
        }
        var totalSum = 0;
        $("#discountBox .realAmount").each(function (index, obj) {
            totalSum += parseFloat($(obj).html());
        });
        $("#realTotalSum2").val(totalSum.toFixed(2));
        $("#newTotalSum").val(totalSum.toFixed(2));
        $("#newPayAmount").val(totalSum.toFixed(2));
        $("#newArrears").val((parseFloat($("#newTotalSum").val()) - parseFloat($("#newPayAmount").val())).toFixed(2));
    } else if ($(obj).find("option:selected").text() == "会员赠送") {
        $(obj).parent().parent().find(".deductionAmount").attr("readonly", false).val(0);
        var amount = parseFloat($(obj).parent().parent().find(".amount").html());
        if ($('#earnest').is(':checked')) {
            var depositAmount = parseFloat($(obj).parent().parent().find(".depositAmount").val());
            if (data > (amount - depositAmount)) {
                $(obj).parent().parent().find(".deductionAmount").val((amount - depositAmount).toFixed(2));
                $(obj).parent().parent().find(".realAmount").html(0);
            } else {
                $(obj).parent().parent().find(".deductionAmount").val(data);
                $(obj).parent().parent().find(".realAmount").html((amount - data - depositAmount).toFixed(2));
            }
        } else {
            if (data > amount) {
                $(obj).parent().parent().find(".deductionAmount").val(amount.toFixed(2));
                $(obj).parent().parent().find(".realAmount").html(0);
            } else {
                $(obj).parent().parent().find(".deductionAmount").val(data);
                $(obj).parent().parent().find(".realAmount").html((amount - data).toFixed(2));
            }
        }
        var totalSum = 0;
        $("#discountBox .realAmount").each(function (index, obj) {
            totalSum += parseFloat($(obj).html());
        });
        $("#realTotalSum2").val(totalSum.toFixed(2));
        $("#newTotalSum").val(totalSum.toFixed(2));
        $("#newPayAmount").val(totalSum.toFixed(2));
        $("#newArrears").val((parseFloat($("#newTotalSum").val()) - parseFloat($("#newPayAmount").val())).toFixed(2));

    } else {
        $.ajax({
            url: Feng.ctxPath + "/payService/getCouponPrice",
            type: "POST",
            data: {
                id: $(obj).val()
            },
            success: function (data) {
                var amount = parseFloat($(obj).parent().parent().find(".amount").html());
                $(obj).parent().parent().find(".deductionAmount").attr("readonly", true);
                if ($('#earnest').is(':checked')) {
                    var depositAmount = parseFloat($(obj).parent().parent().find(".depositAmount").val());
                    if (data > (amount - depositAmount)) {
                        $(obj).parent().parent().find(".deductionAmount").val((amount - depositAmount).toFixed(2));
                        $(obj).parent().parent().find(".realAmount").html(0);
                    } else {
                        $(obj).parent().parent().find(".deductionAmount").val(data);
                        $(obj).parent().parent().find(".realAmount").html((amount - data - depositAmount).toFixed(2));
                    }
                } else {
                    if (data > amount) {
                        $(obj).parent().parent().find(".deductionAmount").val(amount.toFixed(2));
                        $(obj).parent().parent().find(".realAmount").html(0);
                    } else {
                        $(obj).parent().parent().find(".deductionAmount").val(data);
                        $(obj).parent().parent().find(".realAmount").html((amount - data).toFixed(2));
                    }
                }
                var totalSum = 0;
                $("#discountBox .realAmount").each(function (index, obj) {
                    totalSum += parseFloat($(obj).html());
                });
                $("#realTotalSum2").val(totalSum.toFixed(2));
                $("#newTotalSum").val(totalSum.toFixed(2));
                $("#newPayAmount").val(totalSum.toFixed(2));
                $("#newArrears").val((parseFloat($("#newTotalSum").val()) - parseFloat($("#newPayAmount").val())).toFixed(2));
            },
            error: function (data) {
                alert('没有权限，请联系系统管理员!');
                parent.layer.close(parent.layer.getFrameIndex(window.name));
            }
        });
    }
}

var tips;
var toothId;
$("#projectListInfo").on('click', '.toothBox', function (e) {
    e.stopPropagation();
    $(".toothBox").removeClass('tips');
    $(this).addClass('tips');
    toothId = $(this).attr("id");
    var leftUpNum = $(this).find('.leftUp').text();
    var rightUpNum = $(this).find('.rightUp').text();
    var leftDownNum = $(this).find('.leftDown').text();
    var rightDownNum = $(this).find('.rightDown').text();
    tips = layer.open({
        type: 4,
        tips: [1, '#ffffff'],
        closeBtn: 0,
        area: ['auto', 'auto'],
        content: ['<div>' + $("#noneBox").html() + '</div>', '.tips'],
        success: function (layero, index) {
            if (leftUpNum.length > 0) {
                for (var w = 0; w < $(layero).find('.toothTopLeft>span').length; w++) {
                    for (var i = 0; i < leftUpNum.length; i++) {
                        if ($(layero).find('.toothTopLeft>span').eq(w).find('span').text() == leftUpNum.substring(i, i + 1)) {
                            $(layero).find('.toothTopLeft>span').eq(w).find('span').addClass('spanStyle');
                        }
                    }
                }
            }
            if (rightUpNum.length > 0) {
                for (var w = 0; w < $(layero).find('.toothTopRight>span').length; w++) {
                    for (var i = 0; i < rightUpNum.length; i++) {
                        if ($(layero).find('.toothTopRight>span').eq(w).find('span').text() == rightUpNum.substring(i, i + 1)) {
                            $(layero).find('.toothTopRight>span').eq(w).find('span').addClass('spanStyle');
                        }
                    }
                }
            }
            if (leftDownNum.length > 0) {
                for (var w = 0; w < $(layero).find('.toothBottomLeft>span').length; w++) {
                    for (var i = 0; i < leftDownNum.length; i++) {
                        if ($(layero).find('.toothBottomLeft>span').eq(w).find('span').text() == leftDownNum.substring(i, i + 1)) {
                            $(layero).find('.toothBottomLeft>span').eq(w).find('span').addClass('spanStyle');
                        }
                    }
                }
            }
            if (rightDownNum.length > 0) {
                for (var w = 0; w < $(layero).find('.toothBottomRight>span').length; w++) {
                    for (var i = 0; i < rightDownNum.length; i++) {
                        if ($(layero).find('.toothBottomRight>span').eq(w).find('span').text() == rightDownNum.substring(i, i + 1)) {
                            $(layero).find('.toothBottomRight>span').eq(w).find('span').addClass('spanStyle');
                        }
                    }
                }
            }
        }
    });
});
$(document).click(function () {
    layer.close(tips);
});
$(document).on('click', '.tipsUl>li', function (e) {
    e.stopPropagation();
    $(this).addClass('tipsLi').siblings().removeClass('tipsLi');
    if ($(this).index() == 0) {
        $(this).parent().siblings().children('#permanent').show().siblings().hide()
    } else if ($(this).index() == 1) {
        $(this).parent().siblings().children('#deciduous').show().siblings().hide()
    }
});
$(document).on('click', '.toothTopLeft>span', function (e) {
    e.stopPropagation();
    var arr = [];
    var text = $(this).text();
    if ($(this).children().hasClass('spanStyle')) {
        var inputValue = $(".tips .leftUp").text().replace(text, "");
        for (var i = 0; i < inputValue.length; i++) {
            arr.push(inputValue.substring(i, i + 1));
        }
        for (var i = arr.length - 1; i >= 0; i--) {
            for (var j = 0; j < i; j++) {
                if (arr[j + 1] > arr[j]) {
                    var change = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = change
                }
            }
        }
        var str = "";
        for (var i = 0; i < arr.length; i++) {
            str += arr[i]
        }
        $(".tips .leftUp").text(str);
        $(this).children().removeClass('spanStyle');
        if ($(".tips .rightUp").text().length > 0 || $(".tips .leftDown").text().length > 0 || $(".tips .rightDown").text().length > 0) {
            var obj = $(".tips>div").eq(0);
            numReduce(obj)
        } else {
            if (str.length > 0) {
                var obj = $(".tips>div").eq(0);
                numReduce(obj)
            }
        }
    } else {
        $(this).children().addClass('spanStyle');
        var add = $(".tips .leftUp").text() + text;
        for (var d = 0; d < add.length; d++) {
            arr.push(add.substring(d, d + 1));
        }
        for (var i = arr.length - 1; i >= 0; i--) {
            for (var j = 0; j < i; j++) {
                if (arr[j + 1] > arr[j]) {
                    var change = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = change
                }
            }
        }
        var str = "";
        for (var i = 0; i < arr.length; i++) {
            str += arr[i]
        }
        $(".tips .leftUp").text(str);
        if ($(".tips .rightUp").text().length > 0 || $(".tips .leftDown").text().length > 0 || $(".tips .rightDown").text().length > 0) {
            var obj = $(".tips>div").eq(0);
            numAdd(obj)
        } else {
            if (str.length > 1) {
                var obj = $(".tips>div").eq(0);
                numAdd(obj)
            }
        }
    }
});
$(document).on('click', '.toothTopRight>span', function (e) {
    e.stopPropagation();
    var arr = [];
    var text = $(this).text();
    if ($(this).children().hasClass('spanStyle')) {
        var inputValue = $(".tips .rightUp").text().replace(text, "");
        for (var i = 0; i < inputValue.length; i++) {
            arr.push(inputValue.substring(i, i + 1));
        }
        for (var i = arr.length - 1; i >= 0; i--) {
            for (var j = 0; j < i; j++) {
                if (arr[j + 1] < arr[j]) {
                    var change = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = change
                }
            }
        }
        var str = "";
        for (var i = 0; i < arr.length; i++) {
            str += arr[i]
        }
        $(".tips .rightUp").text(str);
        $(this).children().removeClass('spanStyle');
        if ($(".tips .leftUp").text().length > 0 || $(".tips .leftDown").text().length > 0 || $(".tips .rightDown").text().length > 0) {
            var obj = $(".tips>div").eq(0);
            numReduce(obj)
        } else {
            if (str.length > 0) {
                var obj = $(".tips>div").eq(0);
                numReduce(obj)
            }
        }
    } else {
        $(this).children().addClass('spanStyle');
        var add = $(".tips .rightUp").text() + text;
        for (var d = 0; d < add.length; d++) {
            arr.push(add.substring(d, d + 1));
        }
        for (var i = arr.length - 1; i >= 0; i--) {
            for (var j = 0; j < i; j++) {
                if (arr[j + 1] < arr[j]) {
                    var change = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = change
                }
            }
        }
        var str = "";
        for (var i = 0; i < arr.length; i++) {
            str += arr[i]
        }
        $(".tips .rightUp").text(str);
        if ($(".tips .leftUp").text().length > 0 || $(".tips .leftDown").text().length > 0 || $(".tips .rightDown").text().length > 0) {
            var obj = $(".tips>div").eq(0);
            numAdd(obj)
        } else {
            if (str.length > 1) {
                var obj = $(".tips>div").eq(0);
                numAdd(obj)
            }
        }
    }
});
$(document).on('click', '.toothBottomLeft>span', function (e) {
    e.stopPropagation();
    var arr = [];
    var text = $(this).text();
    if ($(this).children().hasClass('spanStyle')) {
        var inputValue = $(".tips .leftDown").text().replace(text, "");
        for (var i = 0; i < inputValue.length; i++) {
            arr.push(inputValue.substring(i, i + 1));
        }
        for (var i = arr.length - 1; i >= 0; i--) {
            for (var j = 0; j < i; j++) {
                if (arr[j + 1] > arr[j]) {
                    var change = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = change
                }
            }
        }
        var str = "";
        for (var i = 0; i < arr.length; i++) {
            str += arr[i]
        }
        $(".tips .leftDown").text(str);
        $(this).children().removeClass('spanStyle');
        if ($(".tips .rightUp").text().length > 0 || $(".tips .leftUp").text().length > 0 || $(".tips .rightDown").text().length > 0) {
            var obj = $(".tips>div").eq(0);
            numReduce(obj)
        } else {
            if (str.length > 0) {
                var obj = $(".tips>div").eq(0);
                numReduce(obj)
            }
        }
    } else {
        $(this).children().addClass('spanStyle');
        var add = $(".tips .leftDown").text() + text;
        for (var d = 0; d < add.length; d++) {
            arr.push(add.substring(d, d + 1));
        }
        for (var i = arr.length - 1; i >= 0; i--) {
            for (var j = 0; j < i; j++) {
                if (arr[j + 1] > arr[j]) {
                    var change = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = change
                }
            }
        }
        var str = "";
        for (var i = 0; i < arr.length; i++) {
            str += arr[i]
        }
        $(".tips .leftDown").text(str);
        if ($(".tips .rightUp").text().length > 0 || $(".tips .leftUp").text().length > 0 || $(".tips .rightDown").text().length > 0) {
            var obj = $(".tips>div").eq(0);
            numAdd(obj)
        } else {
            if (str.length > 1) {
                var obj = $(".tips>div").eq(0);
                numAdd(obj)
            }
        }
    }
});
$(document).on('click', '.toothBottomRight>span', function (e) {
    e.stopPropagation();
    var arr = [];
    var text = $(this).text();
    if ($(this).children().hasClass('spanStyle')) {
        var inputValue = $(".tips .rightDown").text().replace(text, "");
        for (var i = 0; i < inputValue.length; i++) {
            arr.push(inputValue.substring(i, i + 1));
        }
        for (var i = arr.length - 1; i >= 0; i--) {
            for (var j = 0; j < i; j++) {
                if (arr[j + 1] < arr[j]) {
                    var change = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = change
                }
            }
        }
        var str = "";
        for (var i = 0; i < arr.length; i++) {
            str += arr[i]
        }
        $(".tips .rightDown").text(str);
        $(this).children().removeClass('spanStyle');
        if ($(".tips .rightUp").text().length > 0 || $(".tips .leftDown").text().length > 0 || $(".tips .leftUp").text().length > 0) {
            var obj = $(".tips>div").eq(0);
            numReduce(obj)
        } else {
            if (str.length > 0) {
                var obj = $(".tips>div").eq(0);
                numReduce(obj)
            }
        }
    } else {
        $(this).children().addClass('spanStyle');
        var add = $(".tips .rightDown").text() + text;
        for (var d = 0; d < add.length; d++) {
            arr.push(add.substring(d, d + 1));
        }
        for (var i = arr.length - 1; i >= 0; i--) {
            for (var j = 0; j < i; j++) {
                if (arr[j + 1] < arr[j]) {
                    var change = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = change
                }
            }
        }
        var str = "";
        for (var i = 0; i < arr.length; i++) {
            str += arr[i]
        }
        $(".tips .rightDown").text(str);
        if ($(".tips .rightUp").text().length > 0 || $(".tips .leftDown").text().length > 0 || $(".tips .leftUp").text().length > 0) {
            var obj = $(".tips>div").eq(0);
            numAdd(obj)
        } else {
            if (str.length > 1) {
                var obj = $(".tips>div").eq(0);
                numAdd(obj)
            }
        }
    }
});

$("#normal_project").click(function () {
    $("#openingBox").show();
    $("#openingBoxTwo").hide();
    $(this).css({'color': '#FF7E00'}).siblings().css({'color': '#333333'})
});

$("#free_admission").click(function () {
    $(this).css({'color': '#FF7E00'}).siblings().css({'color': '#333333'})
    $("#openingBox").hide();
    $("#openingBoxTwo").show();
});

function getPaySeriveFreeService() {
    $.ajax({
        url: Feng.ctxPath + "/payService/getPaySeriveNameFreeService",
        type: "post",
        data: {
            vipLevelName: $("#vipLevelName").val()
        },
        success: function (data) {
            var txt = '';
            for (var i = 0; i < data.length; i++) {
                if (data[i].membersPayserviceId != 0) {
                    if (data[i].payServicePriceOne == data[i].payServicePriceTwo) {
                        txt += ' <div style="margin: 10px 0;pointer-events:none;background: #F1F1F1;">  <a id="' + data[i].vipPayserviceId + '" href=\"javascript:;\" style="font-size: 14px;" onclick=\"chooseProjectTwo(\'' + data[i].id + '\',\'' + data[i].vipPayserviceId + '\',this)\">&nbsp' + data[i].payServiceName + '&nbsp' + data[i].payServicePriceTwo + '元</a></div>'
                    } else {
                        txt += ' <div style="margin: 10px 0;pointer-events:none;background: #F1F1F1;">  <a id="' + data[i].vipPayserviceId + '" href=\"javascript:;\" style="font-size: 14px;" onclick=\"chooseProjectTwo(\'' + data[i].id + '\',\'' + data[i].vipPayserviceId + '\',this)\">&nbsp' + data[i].payServiceName + '&nbsp' + data[i].payServicePriceOne + '-' + data[i].payServicePriceTwo + '元</a></div>'
                    }
                } else {
                    if (data[i].payServicePriceOne == data[i].payServicePriceTwo) {
                        txt += ' <div style="margin: 10px 0">  <a id="' + data[i].vipPayserviceId + '" href=\"javascript:;\" style="font-size: 14px;" onclick=\"chooseProjectTwo(\'' + data[i].id + '\',\'' + data[i].vipPayserviceId + '\',this)\">&nbsp' + data[i].payServiceName + '&nbsp' + data[i].payServicePriceTwo + '元</a></div>'
                    } else {
                        txt += ' <div style="margin: 10px 0">  <a id="' + data[i].vipPayserviceId + '" href=\"javascript:;\" style="font-size: 14px;" onclick=\"chooseProjectTwo(\'' + data[i].id + '\',\'' + data[i].vipPayserviceId + '\',this)\">&nbsp' + data[i].payServiceName + '&nbsp' + data[i].payServicePriceOne + '-' + data[i].payServicePriceTwo + '元</a></div>'
                    }
                }
            }
            $("#payServiceNameTwo").html(txt);
        },
        error: function (data) {
            alert('没有权限，请联系系统管理员!');
            parent.layer.close(parent.layer.getFrameIndex(window.name))
        }
    })
}

$(function () {

});